<?php

  //////////////////////////////////////////////////////////////////
  // OrbitFAQ  Setup                                              //
  // ---------------                                              //
  //                                                              //
  // Orbit FAQ was solely written and developed by Orbit Services //
  // http://www.orbitservices.net                                 //
  //                                                              //
  // Access the Forum here:                                       //
  // http://forums.orbitservices.net/index.php?c=4                //
  //                                                              //
  // OrbitFAQ utilises the following opensource projects/classes; //
  //  + Fckeditor - http://www.fckeditor.net                      //
  //  + Smarty Template Engine - http://smarty.php.net            //
  //  + Swift Email Class - http://www.swiftmailer.org/           //
  //  + OWASP PHP Filter Project - http://www.owasp.org           //
  //  + MySQL Search Class by Stephen Bartholomew                 //
  //                                                              //
  //////////////////////////////////////////////////////////////////

  echo "<font face=verdana>";

  $step = $_GET[step];
  $posted = $_GET[posted];

  $absolute_path = $_SERVER['SCRIPT_FILENAME'];
  $absolute_path = str_replace ( "/setup/index.php", "", $absolute_path );

  $relative_path = $_SERVER['SCRIPT_NAME'];
  $relative_path = str_replace ( "/setup/index.php", "", $relative_path );

  if($step == '')
    {
      ?>
        <h1>OrbitFAQ Setup</h1>
        Welcome to the OrbitFAQ Setup Program!! This setup will guide you through the setup <br />and configuration of your OrbitFAQ System.<br /><br />

        <a href="index.php?step=1">Start Setup</a>
      <?php

      exit;
    }
  elseif($step == '1')
    {
      ?>
        <h1>Step 1 - Setup Preperation</h1>
        We will now check that your system is ready to be installed<br /><br />

        <table width="300">

      <?php

        $setup_folder = '../setup/';
        if (is_writable($setup_folder)) {
            $setup_result = "<font color='green'>Passed</font>";
        } else {
            $setup_result = "<font color='red'>Failed</font>";
            $failed = 'yes';
        }

        ?>
          <tr>
            <td nowrap valign="top">
              <br />Setup Folder is Writable
            </td>
            <td nowrap colspan="2">
              <br /><?=$setup_result;?>
            </td>
          </tr>

      <?php

        $SkinFullPath = $absolute_path . '/skins/';

        // Get our Skins
        $dh = opendir($SkinFullPath);
        $i=1;
        while (($file = readdir($dh)) !== false) {
            if($file != "." && $file != "..") {
              $skins_folder = $SkinFullPath . $file . '/skins_c';
              $skins_folders[] = $skins_folder;
              //echo $skins_folder;
                if (is_writable($skins_folder)) {
                    $skins_result .= "<font color='green'>$file Passed</font><BR />";
                } else {
                    $skins_result .= "<font color='red'>$file Failed</font><BR />";
                    $failed = 'yes';
                }
            }
        }

        ?>
          <tr>
            <td nowrap valign="top">
              <br /><br />Skins Compile Folder(s) are Writable
            </td>
            <td nowrap>
              <br /><br /><?=$skins_result;?>
            </td>
            <td nowrap>
              <br /><br /><font size=1>Directories:<br />
              <?php
               foreach ($skins_folders as $skins)
                {
                  echo "$skins<br />";
                }
              ?>
              </font>
            </td>
          </tr>

      <?php

        $AdminSkinFullPath = $absolute_path . '/orbitfaq/skins/';

        // Get our Skins
        $dh = opendir($AdminSkinFullPath);
        $i=1;
        while (($file = readdir($dh)) !== false) {
            if($file != "." && $file != "..") {
              $adminskins_folder = $AdminSkinFullPath . $file . '/skins_c';
              $adminskins_folders[] = $adminskins_folder;
                if (is_writable($skins_folder)) {
                    $adminskins_result .= "<font color='green'>$file Passed</font><BR />";
                } else {
                    $adminskins_result .= "<font color='red'>$file Failed</font><BR />";
                    $failed = 'yes';
                }
            }
        }

        ?>
          <tr>
            <td nowrap valign="top">
              <br /><br />Admin Skins Compile Folder(s) are Writable
            </td>
            <td nowrap>
              <br /><br /><?=$adminskins_result;?>
            </td>
            <td nowrap>
              <br /><br /><font size=1>Directories:<br />
              <?php
               foreach ($adminskins_folders as $skins)
                {
                  echo "$skins<br />";
                }
              ?>
              </font>
            </td>
          </tr>

      <?php

        if($failed == '')
          {
            ?>
              <tr><td colspan="3"><br /><br /><a href="index.php?step=2">Step 2</a></td></tr>
            <?php
          }
        else
          {
             ?>
             <tr><td colspan="3"><br /><br /><font color="red"><b>Please resolve the above failed items.</b></font></td></tr>
             <?php

          }

        ?>
        </table>
        <?php
      exit;
    }
  elseif($step == '2')
    {
      ?>
        <h1>Step 2 - Create Configuration File</h1>
        We will now create the configuration file.<br /><br />

        <?php

          if($posted == 'yes')
            {
              $database_hostname = $_POST['database_hostname'];
              $database_username = $_POST['database_username'];
              $database_password = $_POST['database_password'];
              $database_name = $_POST['database_name'];

              $userfiles_absolute = $_POST['userfiles_absolute'];
              $userfiles_relative = $_POST['userfiles_relative'];

              $sql_host_error = "We are having a problem connecting to the Database Host with the crudentials provided. Please try again once you have confirmed the details<BR />";
              $sql_db_error = "We are having a problem connecting to the Database on the specified host. Please try again once you have confirmed the details<BR />";

              // Check connection with DB
              $dbc = mysql_connect ($database_hostname, $database_username, $database_password) OR ( $host_result = $sql_host_error );
              mysql_select_db ($database_name) OR ( $db_result = $sql_db_error );

              // Check File Paths
              if (!file_exists($userfiles_absolute)) {
                  $absolute_result = 'User Files Absolute Does Not Exist<BR />';
              }


              if(($host_result != '')OR($db_result != '')OR($absolute_result != ''))
                {
                  echo "<font color=red>$host_result $db_result $absolute_result</font>";
                }
              else
                {
                  // Create our file if all is ok!
                    $source = 'config.inc.php.new';
                    $destination = 'config.inc.php';

                    if (!$content = fopen($source, 'r')) {
                         $config_result .= "Cannot open source file ($source)<BR />";
                         exit;
                    }

                    $contents = fread($content, filesize($source));
                    fclose($content);

                    $contents = str_replace ( "[HOSTNAME]", "$database_hostname", $contents );
                    $contents = str_replace ( "[DATABASE]", "$database_name", $contents );
                    $contents = str_replace ( "[USERNAME]", "$database_username", $contents );
                    $contents = str_replace ( "[PASSWORD]", "$database_password", $contents );
                    $contents = str_replace ( "[RELATIVE_USERFILES]", "$userfiles_relative", $contents );
                    $contents = str_replace ( "[ABSOLUTE_USERFILES]", "$userfiles_absolute", $contents );

                    // In our example we're opening $filename in append mode.
                    // The file pointer is at the bottom of the file hence
                    // that's where $somecontent will go when we fwrite() it.
                    if (!$handle = fopen($destination, 'w+')) {
                         $config_result .= "Cannot create config file ($destination)<BR />";
                         exit;
                    }

                    // Write $somecontent to our opened file.
                    if (fwrite($handle, $contents) === FALSE) {
                        $config_result .= "Cannot write to config file ($destination)<BR />";
                        exit;
                    }

                    fclose($handle);

                    if($config_result != '')
                      {
                        echo "<font color=red>$config_result</font>";
                      }
                    else
                      {
                        $config_file = 'config.inc.php';
                        $new_config_file = '../incs/config.inc.php';

                        if (!copy($config_file, $new_config_file)) {
                            $copy_failed = '> copy the setup/config.inc.php to incs/config.inc.php.<br />';
                        }


                        $old_folder = '../setup';
                        $new_folder = '../setup_' . date("YmdHis");

                        if (!rename($old_folder, $new_folder)) {
                            $rename_failed = '> Delete the setup folder for extra security';
                        }

                        ?>
                          Setup is Complete! Please do the following to finish:<br />
                          <?php
                            if($copy_failed != '')
                              {
                                echo $copy_failed;
                              }
                          ?>
                          > Set the permission of incs/config.inc.php to the same as the other files<br />
                          <?php
                            if($rename_failed != '')
                              {
                                echo $rename_failed;
                              }
                          ?>
                          > Setup folder renamed to <?=$new_folder;?> folder for extra security<br /><br /><br />

                          Please go to http://www.domain.com/orbitfaq/ and login with the default username and password:<br />
                          demo@domain.com & password
                        <?php
                        exit;

                      }


                }

            }
          else
            {
              $database_hostname = 'localhost';
              $database_username = 'username';
              $database_password = '';
              $database_name = 'orbitfaq';
              $userfiles_absolute = $absolute_path . '/userfiles/';
              $userfiles_relative = $relative_path . '/userfiles/';
            }

        ?>

        <form ACTION="index.php?step=2&posted=yes" METHOD="post" NAME="create_config">
        <table width="300">
          <tr>
            <td nowrap valign="top" colspan="2">
              <h2>Database Settings</h2>
            </td>
          </tr>
          <tr>
            <td nowrap valign="top">
              Database Hostname
            </td>
            <td nowrap>
              <input type="text" name="database_hostname" value="<?=$database_hostname;?>" style="width: 300px;" />
            </td>
          </tr>
          <tr>
            <td nowrap valign="top">
              Database Username
            </td>
            <td nowrap>
              <input type="text" name="database_username" value="<?=$database_username;?>" style="width: 300px;" />
            </td>
          </tr>
          <tr>
            <td nowrap valign="top">
              Database Password
            </td>
            <td nowrap>
              <input type="password" name="database_password" value="<?=$database_password;?>" style="width: 300px;" />
            </td>
          </tr>
          <tr>
            <td nowrap valign="top">
              Database Name
            </td>
            <td nowrap>
              <input type="text" name="database_name" value="<?=$database_name;?>" style="width: 300px;" />
            </td>
          </tr>
          <tr>
            <td nowrap valign="top" colspan="2">
              <br /><h2>Folder Paths</h2>
            </td>
          </tr>
          <tr>
            <td nowrap valign="top">
              Absolute Path to User Files
            </td>
            <td nowrap>
              <input type="text" name="userfiles_absolute" value="<?=$userfiles_absolute;?>" style="width: 300px;" />
            </td>
          </tr>
          <tr>
            <td nowrap valign="top">
              Reletive Path to User Files
            </td>
            <td nowrap>
              <input type="text" name="userfiles_relative" value="<?=$userfiles_relative;?>" style="width: 300px;" />
            </td>
          </tr>
          <tr>
            <td colspan="2" nowrap><br /><br />
              <center><input type="submit" value="Save Settings" /></center>
            </td>
          </tr>
        </table>
        <?php
      exit;
    }

  echo "</font>";

?>
